package com.xbongbong.pro.statistic.model.impl;

import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.domain.dao.BiDataCorrelationDao;
import com.xbongbong.pro.domain.entity.BiDataCorrelationEntity;
import com.xbongbong.pro.statistic.model.BiDataCorrelationModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2019/3/20 10:16
 * @since v1.0
 */
@Service("biDataCorrelationModel")
public class BiDataCorrelationModelImpl implements BiDataCorrelationModel {
    @Resource
    private BiDataCorrelationDao dao;
    @Override
    public Integer insert(BiDataCorrelationEntity entity){
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return dao.insert(entity);
    }
    @Override
    public Integer update(BiDataCorrelationEntity entity){
        long now = DateUtil.getInt();
        entity.setUpdateTime(now);
        return dao.update(entity);
    }

    @Override
    public Integer save(BiDataCorrelationEntity entity){
        if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public void insertBatch(List<BiDataCorrelationEntity> entityList) {
        dao.insertBatch(entityList);
    }

    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return dao.deleteByKey(key , corpid);
    }

    @Override
    public BiDataCorrelationEntity getByKey(Long key, String corpid) {
        return dao.getByKey(key , corpid);
    }

    @Override
    public List<BiDataCorrelationEntity> findEntitys(Map<String ,Object> param){
        return dao.findEntitys(param);
    }

    @Override
    public BiDataCorrelationEntity findByLocation(String corpid, Long appId, Long formId, Integer saasMark, Integer businessType,Integer relTable,Integer location, String linkAttr,String secondLinkAttr) {
        Map<String, Object> param = new HashMap<>();
        param.put("location",location);
        return getBiDataCorrelationEntity(corpid, appId, formId, saasMark, businessType, relTable,linkAttr,secondLinkAttr, param);
    }
    @Override
    public BiDataCorrelationEntity findSlaveLocation(String corpid, Long appId, Long formId, Integer saasMark, Integer businessType,Integer relTable, String linkAttr) {
        Map<String, Object> param = new HashMap<>();
        param.put("locationNot",1);
        return getBiDataCorrelationEntity(corpid, appId, formId, saasMark, businessType, relTable,linkAttr, null,param);
    }

    private BiDataCorrelationEntity getBiDataCorrelationEntity(String corpid, Long appId, Long formId, Integer saasMark, Integer businessType, Integer relTable, String linkAttr,String secondLinkAttr,Map<String, Object> param) {
        param.put("corpid",corpid);
        param.put("appId",appId);
        param.put("formId",formId);
        param.put("saasMark",saasMark);
        param.put("businessType",businessType);
        param.put("relTable",relTable);
        param.put("linkAttr",linkAttr);
        param.put("secondLinkAttr",secondLinkAttr);
        //TODO 不加del是因为后面要复用
        List<BiDataCorrelationEntity> list = findEntitys(param);
        // TODO   list的值应该为1  如果不为1,应该做合并操作
        if(list != null && !list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public Integer getEntitysCount(Map<String ,Object>  param){
        return dao.getEntitysCount(param);
    }

    @Override
    public Integer updateWarehouseTime(Set<Long> idIn) {
        if(CollectionsUtil.isNotEmpty(idIn)){
            return dao.updateWarehouseTime(idIn);
        }else {
            return 0;
        }
    }

    @Override
    public Integer updateSyscAll(Set<Long> idIn) {
        return  dao.updateSyscAll(idIn);
    }
}
